using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using DataObject.Core;
using DataObject.Database;

namespace DataObject.DataAccess
{
    public partial class ContractDAO : DataBaseProcessBase<ContractEntity, OBSDataContext>
    {

        public static ContractDAO CreateInstant()
        {
            return new ContractDAO();
        }

        public List<ContractEntity> SelectListItem(string ContractID, System.DateTime? StartDate, HashSet<string> SortClause)
        {
            int Count = 0;
            return SelectListItem(ContractID, StartDate, null, null, out Count, SortClause);
        }

        public List<ContractEntity> SelectListItem(string ContractID, System.DateTime? StartDate, int? Page, int? Row, out int Count, HashSet<string> SortClause)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from table in db.ContractEntities select table);
                if (!ContractID.Equals(string.Empty))
                    query = query.Where(table => table.ContractID.ToLower().Equals(ContractID.ToLower()));
                if (StartDate.HasValue)
                    query = query.Where(table => table.StartDate >= StartDate.Value.Date);
                for (int i = SortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(SortClause.ElementAt(i));
                Count = query.Count();
                if (Page.HasValue && Row.HasValue)
                    return query.PageIQuery(Page.Value, Row.Value).ToList();
                return query.ToList();
            }
        }

        public void InsertCollection(List<ContractEntity> ls)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.ContractEntities.InsertAllOnSubmit(ls);
                db.SubmitChanges();
            }
        }

        public void Update(ContractEntity item)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                db.UpdateContract(item.ContractID, item.AgentID, item.StartDate, item.ExpirationDate, item.Price, item.CreatedByID, item.CreationDate, item.ModifiedByID, item.ModifiedDate, item.Active);
            }
        }

        public List<ContractEntity> GetContractAll()
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ContractEntities.ToList();
            }
        }

        public ContractEntity GetContractById(string pContractID)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                return db.ContractEntities.SingleOrDefault(a => a.ContractID.Equals(pContractID));
            }
        }

        public List<ContractEntity> SelectListItem(string ContractID, string AgentID, System.DateTime? StartDate, System.DateTime? ExpirationDate, decimal? Price, string CreatedByID, System.DateTime? CreationDate, string ModifiedByID, System.DateTime? ModifiedDate, bool? Active, HashSet<string> SortClause)
        {
            int Count = 0;
            return SelectListItem(ContractID, AgentID, StartDate, ExpirationDate, Price, CreatedByID, CreationDate, ModifiedByID, ModifiedDate, Active, null, null, out Count, SortClause);
        }

        public List<ContractEntity> SelectListItem(string ContractID, string AgentID, System.DateTime? StartDate, System.DateTime? ExpirationDate, decimal? Price, string CreatedByID, System.DateTime? CreationDate, string ModifiedByID, System.DateTime? ModifiedDate, bool? Active, int? Page, int? Row, out int Count, HashSet<string> SortClause)
        {
            using (OBSDataContext db = new OBSDataContext(base.connectionString))
            {
                var query = (from table in db.ContractEntities select table);
                if (!ContractID.Equals(string.Empty))
                    query = query.Where(table => table.ContractID.ToLower().Equals(ContractID.ToLower()));
                if (!AgentID.Equals(string.Empty))
                    query = query.Where(table => table.AgentID.ToLower().Equals(AgentID.ToLower()));
                if (StartDate.HasValue)
                    query = query.Where(table => table.StartDate == StartDate.Value.Date);
                if (ExpirationDate.HasValue)
                    query = query.Where(table => table.ExpirationDate == ExpirationDate.Value.Date);
                if (Price.HasValue)
                    query = query.Where(table => table.Price == Price.Value);
                if (!CreatedByID.Equals(string.Empty))
                    query = query.Where(table => table.CreatedByID.ToLower().Equals(CreatedByID.ToLower()));
                if (CreationDate.HasValue)
                    query = query.Where(table => table.CreationDate == CreationDate.Value.Date);
                if (!ModifiedByID.Equals(string.Empty))
                    query = query.Where(table => table.ModifiedByID.ToLower().Equals(ModifiedByID.ToLower()));
                if (ModifiedDate.HasValue)
                    query = query.Where(table => table.ModifiedDate == ModifiedDate.Value.Date);
                if (Active.HasValue)
                    query = query.Where(table => table.Active == Active.Value);
                for (int i = SortClause.Count - 1; i >= 0; i--)
                    query = query.OrderBy(SortClause.ElementAt(i));
                Count = query.Count();
                if (Page.HasValue && Row.HasValue)
                    return query.PageIQuery(Page.Value, Row.Value).ToList();
                return query.ToList();
            }
        }

    }
}